package com.kx.yooli.kx.wechat.filter;

import com.kx.yooli.kx.wechat.constant.ErrorCodeMsg;
import com.kx.yooli.kx.wechat.entity.message.Message;
import com.kx.yooli.kx.wechat.po.result.AjaxResult;
import com.kx.yooli.kx.wechat.utils.comm.GsonUtils;
import com.kx.yooli.kx.wechat.utils.redis.RedisUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.thymeleaf.util.StringUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

/**
 * @author yangyang
 * 全局注释，后期按需启用
 */
@Component
public class TokenCheckFilter  {
    //extends OncePerRequestFilter
    //白名单
    private static final List<String>  WHITE_LIST = List.of("/api/user/updateUserInfo","/api/user/goRegister","/api/user/goLogin","/api/user/initInfo","/api/message/sendVerifyCode","/api/captcha/render");

    @Resource
    private RedisUtil redisUtil;
    //@Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
/*        String token = (String) request.getHeader("Authorization");
        response.setContentType("application/json; charset=UTF-8");
        if (WHITE_LIST.contains(request.getRequestURI())) {
            filterChain.doFilter(request, response);
        } else if (token == null || !token.startsWith("Bearer ")) {
            PrintWriter writer = response.getWriter();
            writer.write(GsonUtils.toJson(AjaxResult.error(ErrorCodeMsg.TOKEN_NOT_EXIST.getCode(), ErrorCodeMsg.TOKEN_NOT_EXIST.getDesc())));
            writer.close();
        } else if (StringUtils.isEmpty((String) redisUtil.get(token))) {
            PrintWriter writer = response.getWriter();
            writer.write(GsonUtils.toJson(AjaxResult.error(ErrorCodeMsg.TOKEN_EXCEPTION.getCode(), ErrorCodeMsg.TOKEN_EXCEPTION.getDesc())));
            writer.close();
        }*/
    }
}
